package net.chathu.airgw.packets.chat 
{
	import net.chathu.airgw.Config;
	import net.chathu.airgw.Debug;
	import net.chathu.airgw.model.JoinedRoomInfo;
	import net.chathu.airgw.model.Room;
	import net.chathu.airgw.model.User;
	import net.chathu.airgw.packets.gw.serverpackets.PrivMsgPacket;
	import net.chathu.airgw.PrefixManager;
	import net.chathu.airgw.util.StringUtil;
	/**
	 * ...
	 * @author ze
	 */
	public class MRooms extends ChatPacket
	{
		private var roomId:int;
		private var userId:int;
		
		override public function execute():void
		{
			var posA:int = (data[1]).indexOf('/', 0) + 1;
			var posB:int = (data[1]).indexOf('/', 7);
			
			roomId = parseInt(data[1].substring(posA, posB));
			userId = parseInt(data[2]);
			
			var room:Room = sessionData.chatData.rooms.getRoom(roomId);
			
			if (room)
			{
				
				if (userId == -1)
				{
					
					//moderátor üzenet vagy előzmény
					//pl.:
					//>> m|rooms/136/chat|-1|ሴ0907&S00FF00&_&B[Gidu_.I..]&P&_túúúléltem az esküvőt :D
					var msg:String;
					if (String(data[3]).indexOf("moder") == 0 || data[3].indexOf("&_&B") == -1) {
						if (String(data[3]).indexOf("?") == 0)
						{
							msg = " "+Config.IRC_CHAR_REVERSED + String(data[3]).substring(5) + Config.IRC_CHAR_REVERSED;
						} else {
							msg = " "+Config.IRC_CHAR_REVERSED + String(data[3]) + Config.IRC_CHAR_REVERSED;
						}
						
						msg=StringUtil.replaceSpecialCharsToIRCStringFormat(msg);
						sendToClient(new PrivMsgPacket(Config.IRC_HOST, room.name, msg));
					} else {
						try {
							var date:String = data[3].substring(1, 5);
							var posC:int = data[3].indexOf("&_&B");
							var posD:int = data[3].indexOf("&P&_", posC);
							/*
							 * A posC azert kell indexOf-ba, mert ha /me uzenet van
							 * elozmenybe, akkor pl igy nez ki:
							 * ?1439&SFF72E3&P&_&_&BBacisMaci&P&_ =niki amugy: pl:
							 * ?1439&SFF72E3&_&B[BacisMaci]&P&_hehe
							 */

							
							 /**
							 * Az elozmenyben is kicsereljuk az iso-8859-1 karaktereket
							 * iso-8859-2re, hatha valaki olyat irt ott...
							 */
							
							var originalMessage:String = StringUtil.convertFromISO88591(data[3].substring(posD + 4));		
							var source:String = StringUtil.convertFromISO88591(data[3].substring(posC + 4, posD));
							
							// ISMUTED ITT NINCS KEZELVE...

							msg = "["+ Config.IRC_CHAR_COLOR + "8" + Config.IRC_CHAR_BOLD + Config.IRC_CHAR_BOLD + date.substring(0, 2) + ":" + date.substring(2, 4) + Config.IRC_CHAR_COLOR + "] " + source + " " + originalMessage;
							
							
							//sendToClient(new PrivMsgPacket("Előzmény", room.name, msg, 0));
							var pastMsgHost:String = "Előzmény!~elozmeny@elozmeny" + Config.IRC_USER_HOST;
							sendToClient(new PrivMsgPacket(pastMsgHost,room.name,msg));
									
						} catch (err:Error) {
							//ez pedig a moderator action:
							Debug.echo("error: "+ err.message);
						}
					}
				} else {
					//>> m|rooms/136/chat|583664|hm
					var user:User = User(sessionData.chatData.users.getUser(userId));
						
					if (user && userId != sessionData.chatData.userId)
					{
						//az altalunk irt uzeneteket nem jelenitjuk meg..
						if (!sessionData.chatData.isIgnored(user.regId))
						{	
							//nincs mute-olva
							var message:String = data[3];
							if (message.indexOf("/me ") == 0) {//szokozzel
								message = "ACTION " + message.substring(4) + "";
							} else if (message.indexOf("/me") == 0) { //szokoz nelkul
								message = "ACTION " + message.substring(3) + "";
							}
							
							//mute itt sincs kezelve..
							//prefix nincs kezelve
							var jri:JoinedRoomInfo = sessionData.chatData.getJoinedRoomInfo(room.id);
							var prefix:String = PrefixManager.getPrefix(sessionData, user, jri);
							message = StringUtil.replaceSpecialCharsToIRCStringFormat(message);
							//sendToClient(new PrivMsgPacket(prefix+user.userHost,room.name,message));
							//nem kell bele prefix
							sendToClient(new PrivMsgPacket(user.userHost,room.name,message));
						}
					}
				}
				
			} else {
				trace("MRooms msg error, room id: " + roomId);
			}
		}
		
	}

}